//
//  CXHToBidSplashAdapter.m
//  CXHAdSDK-ToBid
//
//  Created by 陈坤 on 2025/4/4.
//

#import "CXHToBidSplashAdapter.h"
#import <WindFoundation/WindFoundation.h>
#import <CXHAdSDK-Core/CXHAdSDKSplashAd.h>

@interface CXHToBidSplashAdapter ()<CXHAdSDKSplashAdDelegate>
@property (nonatomic, weak) id<AWMCustomSplashAdapterBridge> bridge;
@property (nonatomic, strong) CXHAdSDKSplashAd *splashAd;
@property (nonatomic, strong) AWMParameter *parameter;
@property (nonatomic, assign) BOOL isReady;

@end

@implementation CXHToBidSplashAdapter

- (instancetype)initWithBridge:(id<AWMCustomSplashAdapterBridge>)bridge {
    self = [super init];
    if (self) {
        _bridge = bridge;
    }
    return self;
}
- (BOOL)mediatedAdStatus {
    return self.isReady;
}
- (void)loadAdWithPlacementId:(NSString *)placementId parameter:(AWMParameter *)parameter {
    self.parameter = parameter;
    UIView *bottomView = [parameter.extra objectForKey:AWMAdLoadingParamSPCustomBottomView];
    
    self.splashAd = [[CXHAdSDKSplashAd alloc] init];
    self.splashAd.delegate = self;
    self.splashAd.posId = parameter.placementId;
    self.splashAd.setSkipButtonToRightTop = YES;
    self.splashAd.bottomView = bottomView;
    [self.splashAd loadSplashAd];
}
- (void)showSplashAdInWindow:(UIWindow *)window parameter:(AWMParameter *)parameter {
//    UIView *bottomView = [parameter.extra objectForKey:AWMAdLoadingParamSPCustomBottomView];
    [self.splashAd showSplashAdInWindow:window];
}
- (void)didReceiveBidResult:(AWMMediaBidResult *)result {
    if (result.win) {
        [self.splashAd sendWinNotice:result.winnerPrice];
        return;
    }
    // todo: 当胜出时，winnerPrice有值，其它情况不回传胜出价
    [self.splashAd sendLossNotice:result.winnerPrice];
}
- (void)destory {
    [self removeSplashAdView];
}
- (void)removeSplashAdView {
//    self.splashAd = nil;
}

#pragma mark - CXHAdSDKSplashAdDelegate
/**
 开屏广告点击回调, 可能会打开 AppStore, WebView, ThirdApp etc.

 @param splashAd 开屏广告
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
 */
- (void)CXH_splashAdDidClick:(CXHAdSDKSplashAd *)splashAd loadingPageURL:(NSString *)loadingPageURL{
    WindmillLogDebug(@"CSJ", @"%@", NSStringFromSelector(_cmd));
    [self.bridge splashAdDidClick:self];
}

/**
 开屏广告关闭回调, 触发条件: 点击广告, 点击跳过, 超时关闭

 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidClose:(CXHAdSDKSplashAd *)splashAd{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge splashAdDidClose:self];
    self.splashAd = nil;
}

/**
 开屏广告展示回调

 @param splashAd 开屏对象
 */
- (void)CXH_splashAdExposured:(CXHAdSDKSplashAd *)splashAd{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    self.isReady = NO;
    [self.bridge splashAdWillVisible:self];
}

/**
 开屏广告加载成功
 
 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidLoad:(CXHAdSDKSplashAd *)splashAd{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    NSString *price = [NSString stringWithFormat:@"%ld", [self.splashAd getBidPrice]];
    [self.bridge splashAd:self didAdServerResponseWithExt:@{
        AWMMediaAdLoadingExtECPM: price
    }];
    self.isReady = YES;
    [self.bridge splashAdDidLoad:self];
}

/**
 开屏广告加载失败
 
 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidFailToLoad:(CXHAdSDKSplashAd *)splashAd error:(NSError *)error{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    self.isReady = NO;
    [self.bridge splashAd:self didLoadFailWithError:error ext:nil];
}

/**
 *  开屏广告成功展示
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdSuccessPresentScreen:(CXHAdSDKSplashAd *)splashAd{
    
}

/**
 *  开屏广告展示失败
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdFailToPresent:(CXHAdSDKSplashAd *)splashAd withError:(NSError *)error{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    self.isReady = NO;
    [self.bridge splashAdDidShowFailed:self error:error];
}

/**
 *  开屏广告关闭落地页
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdCloseLandingPage:(CXHAdSDKSplashAd *)splashAd{
    
}

@end
